﻿  <EditDataFormTemplateSpec
    xmlns="bb_appfx_editdataformtemplate"
    xmlns:common="bb_appfx_commontypes"
	  ID="83b7e636-d805-4ce9-8d20-87d7a53e80d1" 
	  Name="Constituent Interest CustomID Edit Data Form"
    Description="Constituent Interest Custom ID Edit Data Form"
    Author="Blackbaud Professional Services"
  	DataFormInstanceID="5b1c1ac0-1090-405b-9be4-ece12f45de45" 
    RecordType="ConstituentInterest"
    common:SecurityUIFolder="Service Bus Adapter"
	>


    <SPDataForm>

      <!-- define the edit load SP, which is used to load the data for the form -->
      <LoadImplementation SPName="USR_USP_DATAFORMTEMPLATE_EDITLOAD_CONSTITUENTINTERESTCUSTOMID">
        <common:CreateProcedureSQL>
          <![CDATA[
create procedure dbo.USR_USP_DATAFORMTEMPLATE_EDITLOAD_CONSTITUENTINTERESTCUSTOMID(
  @ID uniqueidentifier,
  @DATALOADED bit = 0 output,
  @TSLONG bigint = 0 output,
  @CUSTOMID nvarchar(255) = null output
)
as

	set nocount on;
  
  declare @CHANGEAGENTID uniqueidentifier;
  declare @CURRENTDATE datetime = getdate();
  exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output
  
	-- be sure to set these, in case the select returns no rows
	set @DATALOADED = 0
	set @TSLONG = 0

	-- populate the output parameters, which correspond to fields on the form.  Note that
	-- we set @DATALOADED = 1 to indicate that the load was successful.  Otherwise, the system
	-- will display a "no data loaded" message.  Also note that we fetch the TSLONG so that concurrency
	-- can be considered.
  
  if exists (select 1 from dbo.USR_CONSTITUENTINTEREST where ID = @ID)
	  select
		  @DATALOADED = 1,
		  @TSLONG = TSLONG,
		  @CUSTOMID = LOOKUPID	
	  from 
      dbo.USR_CONSTITUENTINTEREST
	  where 
      ID = @ID
  else
    select
		  @DATALOADED = 1,
		  @CUSTOMID = ''

	return 0;
				]]>
        </common:CreateProcedureSQL>
      </LoadImplementation>

      <SaveImplementation SPName="USR_USP_DATAFORMTEMPLATE_EDIT_CONSTITUENTINTERESTCUSTOMID">
        <common:CreateProcedureSQL>
          <![CDATA[
create procedure dbo.USR_USP_DATAFORMTEMPLATE_EDIT_CONSTITUENTINTERESTCUSTOMID (
	@ID uniqueidentifier,
	@CHANGEAGENTID uniqueidentifier = null,
	@CUSTOMID nvarchar(255)
)
as

	set nocount on;

	if @CHANGEAGENTID is null  
		exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output
				
	declare @CURRENTDATE datetime
	set @CURRENTDATE = getdate()

	begin try
		-- handle updating the data
    if exists (select 1 from dbo.USR_CONSTITUENTINTEREST where ID = @ID)
      begin
        update 
          dbo.USR_CONSTITUENTINTEREST
        set
			    CUSTOMIDENTIFIER = @CUSTOMID,
			    CHANGEDBYID = @CHANGEAGENTID,
			    DATECHANGED = @CURRENTDATE
		    where 
          ID = @ID
      end
    else
      begin
        insert into 
          dbo.USR_CONSTITUENTINTEREST 
            (ID, CUSTOMIDENTIFIER, DATEADDED, DATECHANGED, ADDEDBYID, CHANGEDBYID) 
        values 
          (@ID, @CUSTOMID, @CURRENTDATE, @CURRENTDATE, @CHANGEAGENTID, @CHANGEAGENTID)
      end
	end try
	begin catch
		exec dbo.USP_RAISE_ERROR
		return 1
	end catch

return 0;
				]]>
        </common:CreateProcedureSQL>
      </SaveImplementation>

    </SPDataForm>

    <!-- describe fields on the form, which correspond to parameters on the SP.  Note that system paramters 
	like the record ID, DATALOADED, TSLONG, and CURRENTAPPUSERID need not be listed. -->
    <common:FormMetaData FixedDialog="true">
      <common:FormFields>
        <common:FormField FieldID="CUSTOMID" Caption="Custom identifier" DataType="String" MaxLength="255" />
      </common:FormFields>

      <common:WebUIComponent>
        <common:WebUI>
          <common:DefaultWebUI/>
        </common:WebUI>
      </common:WebUIComponent>

    </common:FormMetaData>

  </EditDataFormTemplateSpec>